<template>
  <uni-nav-bar title="确认付款" left-icon="arrowleft" @clickLeft="back()"/>
  <view class="contain">
    <view class="text1">您本次需要支付的金额为</view>
    <view class="text2">
      ￥{{ this.total }}
    </view>

    <button @click="pay()" class="button">
      零钱支付
    </button>
  </view>

</template>

<script>
export default {
  data() {
    return {
      total: 0,
      buy: [],
    };
  },
  methods: {
    back() {

      uni.showModal({
        title: '是否放弃支付',
        success: function (res) {
          if (res.confirm) {
            uni.navigateBack({
              delta: 1
            });
          }
        }
      })

    },
    pay() {
      //零钱支付方法
      uni.request({
        url: "http://localhost:9999/nbb/LooseChange/decreaseLooseChange",
        method: 'POST',
        header: {
          'Authorization': uni.getStorageSync('Authorization'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: uni.getStorageSync('userId'),
          looseChange: this.total
        },
        success: (res) => {
          let that = this
          if (res.data.code === 0) {
            uni.showModal({
              title: '支付成功',
              content: '该商品将从购物车删除',
              success: function (res) {
                if (res.confirm) {
                  //从购物车删除方法
                  for (let i = 0; i < that.buy.length; i++) {
                    var deleteShopId = that.buy[i].shopId;
                    uni.request({
                      url: "http://localhost:9999/nbb/Shop/deleteShop",
                      method: 'POST',
                      header: {
                        'Authorization': uni.getStorageSync('Authorization'),
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      data: {
                        shopId: deleteShopId
                      },
                      success: (res) => {
                        if (res.data.code === 0) {
                          console.log("删除购物车成功");
                        } else {
                          console.log("删除购物车失败");
                          console.log(res.data);
                        }
                      },
                      fail() {
                        console.log("获取分类失败");
                      }
                    })
                  }
                  uni.navigateTo({
                    url: '/pages/shop/shop'
                  })
                }
              }
            })
          } else {
            console.log("支付失败");
          }
        },
        fail() {
          console.log("支付失败");
        }
      })
    }
  },
  onLoad(option) {
    this.total = option.money
    this.buy = JSON.parse(decodeURIComponent(option.buys))
    // console.log(this.buy)
  },
}
</script>

<style lang="scss" scoped>
::v-deep .uni-nav-bar-text {
  font-size: 20px;
}

::v-deep .uni-navbar__content_view {
  font-size: 20px;
}

.contain {
  text-align: center;

  .text1 {
    font-size: 25px;
    margin-top: 80px;
    font-weight: 550;
  }

  .text2 {
    margin-top: 50px;
    font-size: 30px;
    font-weight: bolder;
    color: red;
  }

  .button {
    margin-top: 450px;
    color: white;
    background: #58BE6B;
  }
}
</style>
